/*
Copyright (C) 2015 Marien Raat

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <iostream>

template <typename T>
class Normalizer {
public:
    // Normalizes a array of data between the given upper and lower.
    // The array needs to contain at least one element.
    static void normalize(
        // The data array that needs to be normalized
        T *data,
        // The target to put the normalized array in.
        // Can be the same as the data
        T *target,
        // The size of the data array
        unsigned size,
        // The lower and upper value, between which the data needs to
        // be normalized
        T lower,
        T upper) {

        // Implementation
        T max = data[0], min = data[0];
        for (int i = 0; i < size; i++) {
            min = std::min(min, data[i]);;
            max = max > data[i] ? max : data[i];
        }
        for (int i = 0; i < size; i++) {
            target[i] = data[i] - min;
            target[i] /= max - min;
            target[i] *= (upper - lower);
            target[i] += lower;
        }
    }
};
